package com.tongda.menu.activity;

import java.util.ArrayList;
import com.tongda.core.utils.ActivityUtil;
import com.tongda.core.utils.SharedPreferencesUtils;
import com.tongda.menu.R;
import android.os.Bundle;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class IntroduceActivity extends BaseActivity {
	
	private ViewPager mViewPager;	
	private ImageView mPage0;
	private ImageView mPage1;
	private AnimationDrawable drawable1;
	private AnimationDrawable drawable2;	
	private String fromStr = "";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.introduce);
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    	
    	if(getIntent().getStringExtra("from")!=null){
    		fromStr=getIntent().getStringExtra("from");
    	}
        mViewPager = (ViewPager)findViewById(R.id.whatsnew_viewpager);        
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
       
        
        mPage0 = (ImageView)findViewById(R.id.page0);
        mPage1 = (ImageView)findViewById(R.id.page1);
        
        LayoutInflater mLi = LayoutInflater.from(this);
        View view1 = mLi.inflate(R.layout.whats1, null);
        View view2 = mLi.inflate(R.layout.whats2, null);     
        
        view1.setBackgroundResource(R.anim.guide01_bg);
        drawable1 = (AnimationDrawable)view1.getBackground();
        view2.setBackgroundResource(R.anim.guide02_bg);
        drawable2 = (AnimationDrawable)view2.getBackground();
        
    	if(fromStr.equals("Help")){
    		Button passBtn = (Button)view1.findViewById(R.id.passBtn);
    		Button startBtn = (Button)view2.findViewById(R.id.startBtn);
    		passBtn.setText("去点菜");
    		startBtn.setText("去点菜");
    	}
    	
        final ArrayList<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        
        PagerAdapter mPagerAdapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}
			
			@Override
			public int getCount() {
				return views.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager)container).removeView(views.get(position));
			}
			
			
			
			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager)container).addView(views.get(position));
				if(position==0){
					drawable1.start();
				}
				if(position==1){
					drawable2.start();
				}
				return views.get(position);
			}
		};
		
		mViewPager.setAdapter(mPagerAdapter);
    }    
    
	
    public class MyOnPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageSelected(int arg0) {
			switch (arg0) {
			case 0:				
				mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 1:
				mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page));
			}
		}
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}
    public void startbutton(View v) {  
    	if(fromStr.equals("")){
    		setUpdateDishes(true);
			SharedPreferencesUtils.setParam(IntroduceActivity.this, "is_first_install", false);
    	}
		this.finish();
		overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_top);
      }  
    
    public void passbutton(View v) {  
    	if(fromStr.equals("")){
    		setUpdateDishes(true);
    		SharedPreferencesUtils.setParam(IntroduceActivity.this, "is_first_install", false);
    	}
		this.finish();
		overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_top);
      }  
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(keyCode==KeyEvent.KEYCODE_BACK){
    		if(fromStr.equals("")){
        		ActivityUtil.switchActivity(this, false, SplashActivity.class);
    		}
    		this.finish();
    		overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_top);
    	}
    	return super.onKeyDown(keyCode, event);
    }
}
